package com.hjw.smsapp;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hjw.smsapp.base.BaseActivity;
import com.hjw.smsapp.service.SmsObserverService;

public class SettingActivity extends BaseActivity {
    private TextView tvUser;
    private LinearLayout llSmsIp;
    private EditText etSmsIp;
    private TextView tvSmsIp, tvSmsIpSure;
    private SharedPreferences preferences;
    private SharedPreferences.Editor editor;

    @Override
    public int getContentViewId() {
        return R.layout.activity_setting;
    }

    @Override
    public void initView() {
        tvUser = findViewById(R.id.tv_user);

        llSmsIp = findViewById(R.id.ll_sms_ip);
        etSmsIp = findViewById(R.id.et_sms_ip);
        tvSmsIp = findViewById(R.id.tv_sms_ip);
        tvSmsIpSure = findViewById(R.id.tv_sms_ip_sure);

        preferences = getSharedPreferences("data", Context.MODE_PRIVATE);
        editor = preferences.edit();

        String username = preferences.getString("username", "");
        tvUser.setText("当前用户：" + username);
    }

    @Override
    public void initData() {
        tvSmsIp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showSmsIpEdit(true);
            }
        });
        tvSmsIpSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String ip = etSmsIp.getText().toString();
                if (TextUtils.isEmpty(ip)) {
                    showToast("请输入正确的地址");
                } else {
                    editor.putString("smsIp", ip);
                    editor.apply();
                    showSmsIpEdit(false);
                }
            }
        });
    }

    private void showSmsIpEdit(boolean show) {
        if (show) {
            String ip = preferences.getString("smsIp", "");
            etSmsIp.setText(ip);
            llSmsIp.setVisibility(View.VISIBLE);
            tvSmsIp.setVisibility(View.GONE);
        } else {
            llSmsIp.setVisibility(View.GONE);
            tvSmsIp.setVisibility(View.VISIBLE);
        }
    }

    public void onSetIp(View view) {
        Intent intent = new Intent(this, LoginActivity.class);
        intent.putExtra("setIp", true);
        intent.putExtra("autoLogin", false);
        startActivity(intent);
        finish();
    }

    public void onLogin(View view) {
        Intent intent = new Intent(this, LoginActivity.class);
        intent.putExtra("autoLogin", false);
        startActivity(intent);
        finish();
    }

    public void onExit(View view) {
        Intent intent = new Intent(this, SmsObserverService.class);
        stopService(intent);
        SmsApplication.exit();
    }

    public void onBack(View view) {
        finish();
    }
}
